package fi.pakkanen.yarpg.item;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import fi.pakkanen.yarpg.item.trait.Triggerable;

/**
 * A triggerable item.
 * 
 * @author Oliver Michels <oliver.michels@gmx.net>
 * 
 */
public class TriggerableItem extends AbstractItem implements Triggerable {

	/** The valid states for this item */
	private static Set<ItemState> validStates;

	static {
		ItemState[] valids = { ItemState.INACTIVE, ItemState.ACTIVE, ItemState.RECHARGING };
		validStates = new HashSet<ItemState>(Arrays.asList(valids));
	}

	/** THe time how long the item is active when triggered */
	private final int durationTime;

	/** The time to re-charge the item before it can be triggered again */
	private final int rechargeTime;

	/**
	 * Create a triggerable item, that has an instant effect without duration
	 * and doesn't need to be re-charged again after it was used.
	 */
	public TriggerableItem() {
		this(0, 0);
	}

	/**
	 * Create a triggerable item, that takes a given amount of seconds to
	 * re-charge after it was used and before it can be used again
	 * 
	 * @param rechargeTimeInSeconds
	 *            the time to re-charge in seconds
	 */
	public TriggerableItem(final int durationTimeInSeconds, final int rechargeTimeInSeconds) {
		super(ItemState.INACTIVE);

		durationTime = durationTimeInSeconds;
		rechargeTime = rechargeTimeInSeconds;
	}

	@Override
	public boolean isTriggerable() {
		return true;
	}

	@Override
	public void trigger() {
		if (isInactive()) {
			changeState();
		}
	}

	@Override
	public int getRechargetime() {
		return rechargeTime;
	}

	@Override
	public boolean needsRecharging() {
		return getRechargetime() > 0;
	}

	@Override
	public int getDuration() {
		return durationTime;
	}

	@Override
	public boolean hasDuration() {
		return durationTime > 0;
	}

	@Override
	public boolean isRecharging() {
		return isState(ItemState.RECHARGING);
	}

	@Override
	public boolean isActive() {
		return isState(ItemState.ACTIVE);
	}

	@Override
	public boolean isInactive() {
		return isState(ItemState.INACTIVE);
	}

	@Override
	public boolean invoke() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean revert() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected Set<ItemState> getValidStates() {
		return validStates;
	}

}
